/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  io.idc.core.log.publisher.MagicLogPublisher
 *  org.beetl.core.Configuration
 *  org.beetl.core.GroupTemplate
 *  org.beetl.core.ResourceLoader
 *  org.beetl.core.Template
 *  org.beetl.core.resource.StringTemplateResourceLoader
 *  org.springframework.stereotype.Component
 */
package com.chenfan.magic.resolver;

import com.chenfan.magic.core.IdcEngineEntry;
import com.chenfan.magic.core.MagicLogPublisher;
import com.chenfan.magic.entity.TTLDetailModel;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author lm
 */
@Component
public class BeetlResolver
implements __IResolver {
    public static final String TYPE_CODE = "beetl";

    @Override
    public String resolve(Map<String, Object> idcRuntimePool, String templateContent, Map<String, Object> paramMap) {
        TTLDetailModel ttlDetailModel = new TTLDetailModel();
        ttlDetailModel.start();
        String result = "";
        try {
            StringTemplateResourceLoader stringTemplateResourceLoader = new StringTemplateResourceLoader();
            Configuration cfg = Configuration.defaultConfiguration();
            GroupTemplate gt = new GroupTemplate(stringTemplateResourceLoader, cfg);
            Template template = gt.getTemplate(templateContent);
            template.binding(paramMap);
            result = template.render();
            MagicLogPublisher.publishResolverEvent((String)TYPE_CODE, (String)templateContent, (String)result, paramMap, (String)(ttlDetailModel.end().getTtl() + ""));
        }
        catch (IOException e) {
            e.printStackTrace();
            MagicLogPublisher.publishResolverEvent((String)TYPE_CODE, (String)templateContent, (String)result, paramMap, (String)(ttlDetailModel.end().getTtl() + ""), (Throwable)e);
            IdcEngineEntry.throwException(idcRuntimePool, null, e);
        }
        return result;
    }
}

